#!/usr/bin/perl -w
use strict;

use FindBin;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin");
my ($cmd);
use db_parameters;
use ensembl_parameters;
use dbh_helper;
use DBI;
use DBD::mysql;
my $THIS_DIR = "$FindBin::Bin";
$THIS_DIR =~ s/^(.*?)\/*$/$1/;

my $START_VERSION = 30;
my $END_VERSION = 37;

print STDERR <<"HEADLINE";
888888888888888888888888888888888888888888888888888888888888888888888

    download all ensembl schemas
	from version $START_VERSION to version $END_VERSION


888888888888888888888888888888888888888888888888888888888888888888888

HEADLINE

#_________________________________________________________________________________________

#	retrieve_and_load_mysql_tables

#		retrieve mysql data from ENSEMBL

#_________________________________________________________________________________________
sub retrieve_schema($$)
{
	my ($ens_db_name, $ens_db_dir) = @_;




	{
		next if -f $ens_db_name.".sql";
		print STDERR "\tSchema for $ens_db_name\n";
		#
		#	create schema
		#
		my $file = "$ens_db_name.sql.gz";
		system("wget -q $ens_db_dir/$file -P $THIS_DIR") &&
			die "Error:\n\tCouldn't retrieve the sql schema file $ens_db_dir/$file";
	
		#
		#	unzip
		#
		#print STDERR "\tUnzipping $file...\n";
		system("gunzip $THIS_DIR/$file");
	}
}






#_________________________________________________________________________________________

#	retrieve_ensembl_per_taxon_sql_tables

#		retrieve per taxon sql tables from ENSEMBL

#_________________________________________________________________________________________
sub retrieve_ensembl_per_taxon_sql_tables($$)
{
	my ($taxon, $version) = @_;
	#
	#	ftp directory
	#
	my $ens_db_dir = get_ens_taxon_mysql_core_ftp_directory_name($taxon, $version);



	my $ens_db_name = get_enscore_db_name($taxon, $version);
	retrieve_schema($ens_db_name, $ens_db_dir);
}

sub retrieve_ensmart_per_taxon_sql_tables($)
{
	my ($version) = @_;


	#
	#	ftp directory
	#
	my $ens_db_dir = get_ensmart_ftp_directory_name($version);
	my $ens_db_name = get_ensmart_db_name($version);
	retrieve_schema($ens_db_name, $ens_db_dir);
}

#_________________________________________________________________________________________

#	retrieve_ensembl_multi_species_sql_tables

#		retrieve compara sql tables from ENSEMBL

#_________________________________________________________________________________________
sub retrieve_ensembl_multi_species_sql_tables($)
{
	my ($version) = @_;
	#
	#	ftp directory
	#
	my $ens_db_dir = get_enscompara_ftp_directory_name($version);
	my $ens_db_name = get_enscompara_db_name($version);
	retrieve_schema($ens_db_name, $ens_db_dir);
}




#
#	Get version numbers for core/mart/multispecies (i.e. compara)
#

	use Net::FTP;
	my $ftp = Net::FTP->new("ftp.ensembl.org") or die;
	$ftp->login("anonymous", "-anonymous@") or die $ftp->message;;
	my @core;
	my @mart;
	my @multi;
	for my $major_version($START_VERSION..$END_VERSION)
	{
		die unless ($ftp->cwd("/pub/release-$major_version"));
		my @dir = $ftp->dir() or die $ftp->message;
		for (@dir)
		{
			if (/human[-]([a-zA-Z0-9\._\-]+)/ || /homo_sapiens[_]([a-zA-Z0-9\._\-]+)/)
			{
				my $release = "";
				$release = $1;
				$release =~tr/\./_/;
				push(@core,  $release);
			}
			if (/mart[-_]([a-zA-Z0-9\._\-]+)/)
			{
				my $release = "";
				$release = $1;
				$release =~tr/\./_/;
				push(@mart,  $release);
			}
			if (/multi[-_]species[-_]([a-zA-Z0-9\._\-]+)/)
			{
				my $release = "";
				$release = $1;
				$release =~tr/\./_/;
				push(@multi,  $release);
			}
		}
	}




#
#	per taxon sql
#
my @ensemb_db_param = get_ensembl_taxon_data();
{
	for my $taxon_data(@ensemb_db_param)
	{
		next unless $taxon_data->[TAXON] eq 'homo';
	
		#
		#	Retrieve from ENSEMBL
		#
		for (@core)
		{
			retrieve_ensembl_per_taxon_sql_tables($taxon_data, $_);
		}
	
	}
}

	#
	#	Retrieve Ensmart
	#
	for (@mart)
	{
		retrieve_ensmart_per_taxon_sql_tables($_);
	}
	#
	# Retrieve compara
	#
	retrieve_ensembl_multi_species_sql_tables($_) for (@multi);



print STDERR "\tCompleted\n";





